<template>
  <div class="forget-panel">
    <div class="body">
      <a-card class="item">
        <div style="width: 400px">
          <a-form layout="horizontal" ref="step1" :model="form" :rules="rules" :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
            <!-- <a-form-item label="老密码:" name="password0">
              <a-input v-model:value="form.password0" type="password" placeholder="请输入老密码" allow-clear autocomplete="off" :maxlength="11" />
            </a-form-item> -->
            <a-form-item label="新密码:" name="password1">
              <a-input v-model:value="form.password1" type="password" placeholder="请输入新密码" allow-clear autocomplete="off" :maxlength="11" />
            </a-form-item>
            <a-form-item label="确认新密码:" name="password2">
              <a-input v-model:value="form.password2" type="password" placeholder="请确认新密码" allow-clear autocomplete="off" :maxlength="50" />
            </a-form-item>
          </a-form>
          <a-button type="primary" :block="true" @click="toSave">保存</a-button>
        </div>
      </a-card>
    </div>
  </div>
</template>

<script setup name="forget">
import { ref, reactive } from 'vue'
import { QuestionCircleOutlined, IssuesCloseOutlined, CheckCircleOutlined } from '@ant-design/icons-vue'
import { ACCESS_TOKEN, HOME_PAGE_PATH } from '@/mutation-type'
import { webStorage } from '@/utils/webStorage'
import { useUserStore } from '@/store/user'
import router from '@/router'
import { changePwd } from '@/api/authorize'

const step1 = ref(null)
const form = reactive({})
let validatePass = async (_rule, value) => {
  if (!value) {
    return Promise.reject('请输入新密码')
  } else {
    if (form.password2 !== '') {
      step1.value.validateFields('password2')
    }
    return Promise.resolve()
  }
}
let validatePass2 = async (_rule, value) => {
  if (!value) {
    return Promise.reject('请确认新密码')
  } else if (value !== form.password1) {
    return Promise.reject('两次密码不相同!')
  } else {
    return Promise.resolve()
  }
}
const rules = reactive({
  // password0: [{ required: true, message: '请输入老密码', type: 'string', trigger: 'blur' }],
  password1: [
    {
      required: true,
      validator: validatePass,
      trigger: 'blur',
    },
  ],
  password2: [
    {
      validator: validatePass2,
      trigger: 'change',
    },
  ],
})

const toLogin = () => router.push(`/user`)
const toSave = async () => {
  // 校验表单
  await step1.value.validate()

  await changePwd({ password: form.password1, password1: form.password2 })
}
</script>

<style lang="less" scoped>
@import url('@/assets/style/authorize.less');
.forget-panel {
  width: 100%;
  height: 100%;
  background: #f6f7fa;
  text-align: left;
  display: flex;
  flex-flow: column nowrap;
  // justify-content:flex-start;
  padding-top: 50px;
  align-items: center;
  .body {
    display: flex;
    justify-content: center;
    align-items: center;
    .title {
      font-size: 14px;
      font-weight: bold;
      span {
        color: #e1251b;
        cursor: pointer;
      }
    }
    .item {
      width: 800px;
      height: 400px;
      padding: 20px 0;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
